package com.logisticsmanagement.services;

import static com.logisticsmanagement.utils.GlobelValue.*;

import java.util.ArrayList;
import java.util.List;

import com.logisticsmanagement.dao.CityDao;
import com.logisticsmanagement.dao.IndentDao;
import com.logisticsmanagement.dao.TruckDao;
import com.logisticsmanagement.dao.UserDao;
import com.logisticsmanagement.domain.City;
import com.logisticsmanagement.domain.Route;
import com.logisticsmanagement.domain.Truck;
import com.logisticsmanagement.domain.User;
import com.logisticsmanagement.utils.GlobelValue;
import com.logisticsmanagement.vo.BaseResponse;
import com.logisticsmanagement.vo.VoIndent;
import com.logisticsmanagement.vo.VoRoute;
import com.logisticsmanagement.vo.VoTruck;
import com.logisticsmanagement.vo.VoUser;

public class TruckService {
	private TruckDao truckDao=new TruckDao();
	private CityDao cityDao = new CityDao();
	private UserDao userDao=new UserDao();
	
	
	private IndentDao indentDao=new IndentDao();
	private IndentService indentService=new IndentService();
	
	
	public BaseResponse<VoTruck> buyTruck(VoTruck inputVoTruck){
		
		//
		int originCityID = inputVoTruck.getOriginCityID();
		double truckPrice = inputVoTruck.getTruckPrice();
		
		if(truckPrice<=1000){
			return new BaseResponse<VoTruck>(GlobelValue.RESULT_DATA_ILLEGAL,"you need give me a truck price,or your price value not illegal");
		}
		if(cityDao.selectById(originCityID)== null){
			return new BaseResponse<VoTruck>(GlobelValue.RESULT_FOREIN_KEY_CONSTRAINT_FAILED, GlobelValue.RESULT_FOREIN_KEY_CONSTRAINT_FAILED_DESCRIPTON);
		}
		Truck truck = VoTruck2Truck(inputVoTruck);
		truck.setTruckID(null);
		truck.setFromCityID(null);
		truck.setNextCityID(null);
		
		truck =truckDao.add(truck);
		if(truck == null){
			return new BaseResponse<VoTruck>(GlobelValue.RESULT_SERVER_ERROR, GlobelValue.RESULT_SERVER_ERROR_DESCRIPTION);
		}
		ArrayList<VoTruck> list = new ArrayList<VoTruck>();
		list.add(Truck2VoTruck(truck));
		return new BaseResponse<VoTruck>(GlobelValue.RESULT_OK, GlobelValue.RESULT_OK_DESCRIPTON,list);
	}
	
	/**
	 * query a special truck through truckID,
	 * and return a VoTruck.
	 * @param truckID
	 */
	public BaseResponse<VoTruck> queryTruck(int truckID){
		
		Truck truck=truckDao.selectByID(truckID);	
		if(truck == null){
			return new BaseResponse<VoTruck>(GlobelValue.RESULT_REQUEST_DATA_NOT_EXIST, GlobelValue.RESULT_REQUEST_DATA_NOT_EXIST_DESCRIPTION);
		}
		List<VoTruck> listVoTruck=new ArrayList<VoTruck>();
		listVoTruck.add(Truck2VoTruck(truck));
	
		return new BaseResponse<VoTruck>(GlobelValue.RESULT_OK, GlobelValue.RESULT_OK_DESCRIPTON,listVoTruck);	

	}
	
	/**
	 * update special truck into database
	 * @param newUser
	 * @return
	 */
	public boolean updateTruckInfo(Truck newTruck){	
		//before modify truck information;first you shoule be a driver or manager.
		
		if(newTruck.getDriverID()!=null){//if a driver
			
			
		}
		return truckDao.update(newTruck);
	}
	
	/**
	 * delete Truck through truckID.
	 * @param truckID
	 * @return
	 */
	public boolean deleteTruck(int truckID){
		//before set attribute isdelete=false;
		//first search all using truck in table to ensure the car not in indent.truckID
		String driverID=null;
		truck=truckDao.selectByID(truckID);
		if(null==(driverID=truck.getDriverID())){//means do not have conflict when delete truck
			truck.setIsDelete(true);
			truckDao.update(truck);
			
			return true;//means success to delete
		}else{//means this truck is using
			truck.setPrepareDelete(true);
			truckDao.update(truck);
			return false;//means fail to delete
		}
	}

	public boolean updateIndents(String driverID){
		boolean flag=false;
		User driver=userDao.selectByID(driverID);
		//jundge whether driver is legal. 
		if(driver.getRole().equalsIgnoreCase("driver")&&
				driver.getUserID()!=null){
			
			Truck newTruck=new Truck();
			List<Truck> truckList=new ArrayList<Truck>();
			
			newTruck.setDriverID(driver.getUserID());
			
			truckList=truckDao.selectPage(newTruck, 1, PAGE_SIZE);
			if(truckList.size()==1){//means legal
				for(int i=0;i<truckList.size();i++){
					flag=indentService.driverUpdateIndents(truckList.get(i));
				}

			}else{
				return flag;
			}
		}		
		return flag;
	}

	public BaseResponse<VoUser> updateTruck(VoUser voDriver){
		
		if(voDriver.getUserID()==null||voDriver.getRole()==null
				||voDriver.getPassword()==null){
			return new BaseResponse<VoUser>(GlobelValue.QUERY_NOT_FOUND,GlobelValue.QUERY_NOT_FOUND_DESCRIPTON);
		}
		if(!voDriver.getRole().equalsIgnoreCase("driver")){
			return new BaseResponse<VoUser>(GlobelValue.QUERY_NOT_FOUND,GlobelValue.QUERY_NOT_FOUND_DESCRIPTON);

		}
		Truck truck=new Truck();
		truck.setDriverID(voDriver.getUserID());
		
		List<Truck> queryTruck = truckDao.selectPage(truck, 1, 1);
		
		if(queryTruck.size() ==0){
			//query failed ,no special truck
			return new BaseResponse<VoUser>(GlobelValue.QUERY_NOT_FOUND, GlobelValue.QUERY_NOT_FOUND_DESCRIPTON);
		}
		truck = queryTruck.get(0);
		//driver has arrived a new city,so update truck info.
	//	truck????????????????????
		
	
		
		//VoUser voUser = User2VoUser(user);
		
		//ArrayList<VoUser> list = new ArrayList<VoUser>();
		
		
		//list.add(voUser);
return null;
	}
	
	private Truck VoTruck2Truck(VoTruck voTruck){
		
		Truck truck = new Truck();
		truck.setCarNumber(voTruck.getCarNumber());
		truck.setDriverID(voTruck.getDriverID());
		truck.setFromCityID(voTruck.getFromCityID());
		truck.setNextCityID(voTruck.getNextCityID());
		truck.setOriginCityID(voTruck.getOriginCityID());
		truck.setTruckID(voTruck.getTruckID());
		truck.setTruckPrice(voTruck.getTruckPrice());
		
		truck.setPrepareDelete(false);
		truck.setIsDelete(false);
		
		return truck;
	}
	
	private VoTruck Truck2VoTruck(Truck truck){
		VoTruck voTruck = new VoTruck();
		voTruck.setCarNumber(truck.getCarNumber());
		voTruck.setDriverID(truck.getDriverID());
		voTruck.setFromCityID(truck.getFromCityID());
		voTruck.setNextCityID(truck.getNextCityID());
		voTruck.setOriginCityID(truck.getOriginCityID());
		voTruck.setTruckID(truck.getTruckID());
		voTruck.setTruckPrice(voTruck.getTruckPrice());
		
		City originCity = cityDao.selectById(truck.getOriginCityID());
		if(originCity !=null){
			voTruck.setOriginCityName(originCity.getName());
		}
		
		City fromCity = cityDao.selectById(truck.getFromCityID());
		if(fromCity != null){
			voTruck.setFromCityName(fromCity.getName());
		}
		
		City nextCity = cityDao.selectById(truck.getNextCityID());
		if(nextCity != null){
			voTruck.setNextCityName(nextCity.getName());
		}
		
		User driver = userDao.selectByID(truck.getDriverID());
		if(driver != null){
			voTruck.setDriverName(driver.getDisplayName());
		}
		
		return voTruck;
	}
}
